@using CleanArchitecture.Blazor.Application.Common.Exceptions
@using System.Net
@inject IStringLocalizer<SharedResource> L
<MudPaper Class="py-4 px-6 rounded-lg d-flex flex-column">
    <MudText Typo="Typo.h6" Color="Color.Error">@statusCode</MudText>
    <div class="d-flex align-center gap-4">
        <MudText Typo="Typo.subtitle2" >@DateTime.Now</MudText>
        <MudButton Variant="Variant.Text" Color="Color.Primary" OnClick="@(()=>_showstacktrace=!_showstacktrace)">More</MudButton>
    </div>
    <MudText>@message For more information, contact your system administrator</MudText>
    @if (_showstacktrace)
    {
        <MudText Typo="Typo.body2" Class="mt-2">@stacktrace</MudText>
    }
    <MudDivider Class="my-3" />
    <div class="mx-n2 mb-2">
        <MudButton Variant="Variant.Outlined" Color="Color.Primary" OnClick="OnRefresh">@L["Refresh"]</MudButton>
    </div>
    <div class="d-flex justify-end">
    </div>
</MudPaper>

@code {
    private string? message { get; set; }
    private bool _showstacktrace { get; set; }
    private string? stacktrace { get; set; }
    private string? statusCode { get; set; } = HttpStatusCode.InternalServerError.ToString();
    [EditorRequired][Parameter] public Exception Exception { get; set; } = default!;
    [Inject] private NavigationManager _navigation { get; set; } = default!;

    protected override Task OnInitializedAsync()
    {
        switch (Exception)
        {
            case CustomException e:
                statusCode = e.StatusCode.ToString();
                if (e.ErrorMessages is not null)
                {
                    message = string.Join(", ", e.ErrorMessages.ToArray());
                }
                break;
            default:
                if (Exception.InnerException != null)
                {
                    while (Exception.InnerException != null)
                    {
                        Exception = Exception.InnerException;
                    }
                }
                message = Exception.Message;
                break;
        }
        stacktrace = Exception.StackTrace;
        return base.OnInitializedAsync();
    }
    private void OnRefresh()
    {
        _navigation.NavigateTo(_navigation.Uri, true);
    }
}
